<?php 
class KF_Sort 
{ 
    var $key;    //key in your array 

    //runs the sort, and returns sorted array 
    public function run ($myarray, $key_to_sort, $type_of_sort = '') 
    { 
        $this->key = $key_to_sort; 
        
        if ($type_of_sort == 'desc') 
            uasort($myarray, array($this, 'myreverse_compare')); 
        else 
            uasort($myarray, array($this, 'mycompare')); 
            
        return $myarray; 
    } 
    
    //for ascending order  
    protected function mycompare($x, $y) 
    { 
        if ( $x[$this->key] == $y[$this->key] ) 
            return 0; 
        else if ( $x[$this->key] < $y[$this->key] ) 
            return -1; 
        else 
            return 1; 
    } 
    
    //for descending order 
    protected function myreverse_compare($x, $y) 
    { 
        if ( $x[$this->key] == $y[$this->key] ) 
            return 0; 
        else if ( $x[$this->key] > $y[$this->key] ) 
            return -1; 
        else 
            return 1; 
    } 
} 
?>